package com.ly.heart.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.OfficeSuppliesDetailEntity;
import com.ly.heart.domain.dto.OfficeSuppliesRecordQueryDto;
import com.ly.heart.mapper.OfficeSuppliesRecordMapper;
import com.ly.heart.service.IOfficeSuppliesDetailService;
import com.ly.heart.service.IOfficeSuppliesRecordService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.OfficeSuppliesRecordEntity;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 办公用品操作记录Service业务层处理
 *
 * @author cxl
 * @date 2024-06-09
 */
@Service
public class OfficeSuppliesRecordServiceImpl extends ServiceImpl<OfficeSuppliesRecordMapper,OfficeSuppliesRecordEntity> implements IOfficeSuppliesRecordService {

    @Resource
    private OfficeSuppliesRecordMapper officeSuppliesRecordMapper;
    @Resource
    private IOfficeSuppliesDetailService officeSuppliesDetailService;

    /**
     * 查询办公用品操作记录
     *
     * @param id 办公用品操作记录ID
     * @return 办公用品操作记录
     */
    @Override
    public OfficeSuppliesRecordEntity selectOfficeSuppliesRecordEntityById(String id)
    {
        return officeSuppliesRecordMapper.selectOfficeSuppliesRecordEntityById(id);
    }

    /**
     * 查询办公用品操作记录列表
     *
     * @param officeSuppliesRecordEntity 办公用品操作记录
     * @return 办公用品操作记录
     */
    @Override
    public List<OfficeSuppliesRecordEntity> selectOfficeSuppliesRecordEntityList(OfficeSuppliesRecordEntity officeSuppliesRecordEntity)
    {
        return officeSuppliesRecordMapper.selectOfficeSuppliesRecordEntityList(officeSuppliesRecordEntity);
    }

    /**
     * 新增办公用品操作记录
     *
     * @param officeSuppliesRecordEntity 办公用品操作记录
     * @return 结果
     */
    @Override
    public int insertOfficeSuppliesRecordEntity(OfficeSuppliesRecordEntity officeSuppliesRecordEntity)
    {
        String recordId = SnowflakeIdWorker.getInstance().nextStringId();
        Date nowDate = DateUtils.getNowDate();
        String userId = SecurityUtils.getLoginUser().getUser().getUserId();
        officeSuppliesRecordEntity.setId(recordId);
        officeSuppliesRecordEntity.setCreateTime(nowDate);
        officeSuppliesRecordEntity.setUserId(userId);
        if(!CollectionUtils.isEmpty(officeSuppliesRecordEntity.getDetails())){
            officeSuppliesRecordEntity.getDetails().forEach(detail -> {
                detail.setRecordId(recordId);
                detail.setOfficeSuppliesId(officeSuppliesRecordEntity.getOfficeSuppliesId());
                detail.setCreateTime(nowDate);
                detail.setUserId(userId);
                officeSuppliesDetailService.save(detail);
            });
        }
        return officeSuppliesRecordMapper.insertOfficeSuppliesRecordEntity(officeSuppliesRecordEntity);
    }

    /**
     * 修改办公用品操作记录
     *
     * @param officeSuppliesRecordEntity 办公用品操作记录
     * @return 结果
     */
    @Override
    public int updateOfficeSuppliesRecordEntity(OfficeSuppliesRecordEntity officeSuppliesRecordEntity)
    {
        officeSuppliesRecordEntity.setChangeTime(DateUtils.getNowDate());
        if(!CollectionUtils.isEmpty(officeSuppliesRecordEntity.getDetails())){
            officeSuppliesRecordEntity.getDetails().forEach(detail -> {
                detail.setChangeTime(DateUtils.getNowDate());
                officeSuppliesDetailService.updateById(detail);
            });
        }
        return officeSuppliesRecordMapper.updateOfficeSuppliesRecordEntity(officeSuppliesRecordEntity);
    }

    /**
     * 批量删除办公用品操作记录
     *
     * @param ids 需要删除的办公用品操作记录ID
     * @return 结果
     */
    @Override
    public int deleteOfficeSuppliesRecordEntityByIds(String[] ids)
    {
        for(String recordId : ids){
            officeSuppliesDetailService.remove(new QueryWrapper<OfficeSuppliesDetailEntity>().eq("record_id",recordId));
        }
        return officeSuppliesRecordMapper.deleteOfficeSuppliesRecordEntityByIds(ids);
    }

    /**
     * 删除办公用品操作记录信息
     *
     * @param id 办公用品操作记录ID
     * @return 结果
     */
    @Override
    public int deleteOfficeSuppliesRecordEntityById(String id)
    {
        officeSuppliesDetailService.remove(new QueryWrapper<OfficeSuppliesDetailEntity>().eq("record_id",id));
        return officeSuppliesRecordMapper.deleteOfficeSuppliesRecordEntityById(id);
    }

    private QueryWrapper<OfficeSuppliesRecordEntity> baseQuery(OfficeSuppliesRecordQueryDto queryDto){
        QueryWrapper<OfficeSuppliesRecordEntity> query = Wrappers.query();
        query.eq("office_supplies_id",queryDto.getOfficeSuppliesId());
        query.eq("type",queryDto.getType());
        return query;
    }

    @Override
    public AjaxResult getOfficeSuppliesRecordByOfficeSuppliesId(OfficeSuppliesRecordQueryDto queryDto){
        QueryWrapper<OfficeSuppliesRecordEntity> query = this.baseQuery(queryDto);
        List<OfficeSuppliesRecordEntity> recordList = this.list(query);
        if(!CollectionUtils.isEmpty(recordList)){
            recordList.forEach(record -> {
                List<OfficeSuppliesDetailEntity> detailList = officeSuppliesDetailService.list(new QueryWrapper<OfficeSuppliesDetailEntity>().eq("record_id", record.getId()));
                record.setDetails(detailList);
            });
        }
        Page<OfficeSuppliesRecordEntity> page = new Page<>(queryDto.getPage(),queryDto.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,recordList));
    }

}
